package com.bandit.remoting.dto;

import com.bandit.emums.RpcResponseCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author Bandit
 * @create 2022/2/23 16:19
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class RpcResponse<T> implements Serializable {

    private static final long serialVersionUID = -8181412241963407191L;
    private String requestId;
    /**
     * response code
     */
    private Integer code;
    /**
     * response message
     */
    private String message;
    /**
     * response body
     */
    private T data;

    public static <T> RpcResponse<T> success(T data, String requestId){
        RpcResponseBuilder<T> responseBuilder = new RpcResponseBuilder<T>();
        return responseBuilder.code(RpcResponseCodeEnum.SUCCESS.getCode())
                .message(RpcResponseCodeEnum.SUCCESS.getMessage())
                .requestId(requestId)
                .data(data)
                .build();
    }

    public static <T> RpcResponse<T> fail(){
        RpcResponseBuilder<T> responseBuilder = new RpcResponseBuilder<T>();
        return responseBuilder.code(RpcResponseCodeEnum.FAIL.getCode())
                .message(RpcResponseCodeEnum.FAIL.getMessage())
                .build();
    }
}
